<?php

/*
 * Action 4$
 *
 * +1 Action. 
 * Reveal the top 3 cards of your deck. The player to your left chooses one of them. 
 * Discard that card. Put the other cards into your hand.
 * 
 */

class Advisor extends Card implements iAction, iKingdom, iGuilds {

    public function __construct() {
        parent::__construct("Advisor", 4);
        $this->undoPlay = false;
        $this->undoBuy = false;
    }

    public function getDescription() {
        return array(
            "+1 action",
            "Reveal the top 3 cards of your deck. The player to your left chooses one of them. ",
            "Discard that card. Put the other cards into your hand.");
    }

    public function play(Game $game, $isRealCard) {
        $game->addActions(1);
        ScreenDisplay::addScreenMessage($game, true, "get +1 action ", "gets +1 action");

        $cards = $game->removeCardsFromDrawDeck(3);
        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($cards), "reveals " . ScreenDisplay::$LMSG);
        $unique_cards = getUniqueCards($cards);

        if (sizeof($unique_cards) > 1) {
            $name = $game->getNextPlayerName();
            $oldPhase = $game->getPhase();
            $game->setPhase(Phase_Sleep);
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $unique_cards, 1, 1, null, "Choose card to discard", $name, true);

            //the waiting function
            $users_replies = MySocket::getUsersReplies($game);
            $card_name = "";
            foreach ($users_replies as $name => $output) {

                $card_name = $output[0];
                $game->setPhase(Phase_Sleep, $name);
            }
            
            $game->setPhase($oldPhase);
            
            $choosen_card = n2c($card_name);
            for ($i = 0; $i < sizeof($cards); $i++) {
                if ($choosen_card->getKey() == $cards[$i]->getKey()) {
                    array_splice($cards, $i, 1);
                    break;
                }
            }
            $game->putCardInDiscardDeck($choosen_card);
            $game->putCardsInPlayHand($cards);
            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($choosen_card) . " and put " . nicePrint($cards) . " in your hand", "discards " . nicePrint($choosen_card) . " and puts " . nicePrint($cards) . " in his hand");
        } elseif (sizeof($unique_cards) == 1) {
            $choosen_card = $cards[0];

            for ($i = 0; $i < sizeof($cards); $i++) {
                if ($choosen_card->getKey() == $cards[$i]->getKey()) {
                    array_splice($cards, $i, 1);
                    break;
                }
            }
            $game->putCardInDiscardDeck($choosen_card);
            $game->putCardsInPlayHand($cards);
            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($choosen_card) . " and put " . nicePrint($cards) . " in your hand", "discards " . nicePrint($choosen_card) . " and puts " . nicePrint($cards) . " in his hand");
        } else {
            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($cards) . " and put nothing in your hand", "discards " . ScreenDisplay::$LMSG . " and puts nothing in his hand");

            $game->discardTempDiscardCards($cards);
        }
        $game->cardActionFinished();
        return true;
    }

}

?>